package com.epam.facebook_client.wrapper;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import common.wrapper.JSONObjectWrapper;

import android.util.Log;

public class User extends JSONObjectWrapper {

	// ----fields-----
	protected static final String ID_FIELD = "id";
	protected static final String NAME_FIELD = "name";
	protected static final String EMAIL_FIELD = "email";
	protected static final String BIRTHDAY_FIELD = "birthday";
	protected static final String GENDER_FIELD = "gender";
	protected static final String BIO_FIELD = "bio";
	protected static final String QUOTES_FIELD = "quotes";
	protected static final String TYPE_FIELD = "type";
	protected static final String DESCRIPTION_FIELD = "description";

	// ----objects-----
	protected static final String HOMETOWN_OBJ = "hometown";
	protected static final String LOCATION_OBJ = "location";
	protected static final String EMPLOYER_OBJ = "employer";
	protected static final String POSITION_OBJ = "position";
	protected static final String CONCENTRATION_OBJ = "concentration";
	protected static final String SCHOOL_OBJ = "school";
	protected static final String YEAR_OBJ = "year";

	// ----arrays-----
	protected static final String WORK_ARRAY = "work";
	protected static final String EDUCATION_ARRAY = "education";
	protected static final String LANGUAGES_ARRAY = "languages";

	private static final String TAG = User.class.getSimpleName();

	public User() {
		super();
	}

	public User(String json) {
		super(json);
	}
	
	public User(String id, String name){
		
	}

	public String getId() {
		return getString(ID_FIELD);
	}

	public String getName() {
		return getString(NAME_FIELD);
	}

	public String getEmail() {
		return getString(EMAIL_FIELD);
	}

	public String getBirthday() {
		return getString(BIRTHDAY_FIELD);
	}

	public String getGender() {
		return getString(GENDER_FIELD);
	}

	public String getBio() {
		return getString(BIO_FIELD);
	}

	public String getQuotes() {
		return getString(QUOTES_FIELD);
	}

	public String getHometown() {
		return getJSONObject(HOMETOWN_OBJ).getString(NAME_FIELD);
	}

	public String getLocation() {
		return getJSONObject(LOCATION_OBJ).getString(NAME_FIELD);
	}

	public List<String> getLanguages() {
		JSONArray languages = getJSONArray(LANGUAGES_ARRAY);
		List<String> listLanguages = new ArrayList<String>();
		for (int i = 0; i < languages.length(); i++) {
			try {
				listLanguages.add(languages.getJSONObject(i).getString(NAME_FIELD));
			} catch (JSONException e) {
				Log.e(TAG, "JSON Exception", e);
			}
		}
		return listLanguages;
	}

	public List<Work> getWork() {
		JSONArray works = getJSONArray(WORK_ARRAY);
		List<Work> listWorks = new ArrayList<User.Work>();
		for (int i = 0; i < works.length(); i++) {
			try {
				listWorks.add(new Work(works.getJSONObject(i).toString()));
			} catch (JSONException e) {
				Log.e(TAG, "JSON Exception", e);
			}
		}
		System.out.println(listWorks.size());
		return listWorks;
	}

	public List<Education> getEducation() {
		JSONArray educatuons = getJSONArray(EDUCATION_ARRAY);
		List<Education> listEducations = new ArrayList<User.Education>();
		if (educatuons != null) {
			for (int i = 0; i < educatuons.length(); i++) {
				try {
					listEducations.add(new Education(educatuons
							.getJSONObject(i).toString()));
				} catch (JSONException e) {
					Log.e(TAG, "JSON Exception", e);
				}
			}
		}
		return listEducations;
	}

	public class Work extends JSONObjectWrapper {

		public Work() {
			super();
		}

		public Work(String json) {
			super(json);
		}

		public String getEmployer() {
			return getJSONObject(EMPLOYER_OBJ).getString(NAME_FIELD);
		}

		public String getLocation() {
			return getJSONObject(LOCATION_OBJ).getString(NAME_FIELD);
		}

		public String getPosition() {
			return getJSONObject(POSITION_OBJ).getString(NAME_FIELD);
		}

		public String getDesctiption() {
			return getString(DESCRIPTION_FIELD);
		}
	}

	public class Education extends JSONObjectWrapper {

		public Education() {
			super();
		}

		public Education(String json) {
			super(json);
		}

		public String getType() {
			return getString(TYPE_FIELD);
		}

		public String getSchool() {
			return getJSONObject(SCHOOL_OBJ).getString(NAME_FIELD);
		}

		public String getYear() {
			JSONObjectWrapper jsonObject = getJSONObject(YEAR_OBJ);
			if (jsonObject != null) {
				return jsonObject.getString(NAME_FIELD);
			}
			return null;
		}

		public String getConcentration() {
			return getJSONObject(CONCENTRATION_OBJ).getString(NAME_FIELD);
		}
	}
}
